package pnu.ssm.controller.manager;
import java.util.ArrayList;
import java.util.Iterator;

import pnu.ssm.model.stuff.Transporter;


// Singleton Pattern
// Iterator Pattern
public class TransporterManager implements Iterable<Transporter>{
	private ArrayList<Transporter> transporters;
	
	private static TransporterManager instance = new TransporterManager();	
	private TransporterManager() {
		transporters = new ArrayList<Transporter>();
	}
	
	public void init(){
		transporters.clear();
	}
	
	public static TransporterManager getInstance(){
		return instance;
	}
	
	public void addTransporter(Transporter t){
		transporters.add(t);
	}
	
	public int getTransporterCount(){
		return transporters.size();
	}
	
	public boolean isAllIdle(){
		boolean allIdle = true;
		Iterator<Transporter> it = iterator();
		while(it.hasNext()){
			Transporter t = it.next();
			if(t.isWorking()){ 
				return false;
			}
		}
		return allIdle;
	}
	
	public Transporter getIdleTransporter(){
		Iterator<Transporter> it = iterator();
		while(it.hasNext()){
			Transporter t = it.next();
			if(!t.isWorking()){
				return t;
			}
		}
		return null;
	}
	
	public Transporter getTransporter(int index){
		if(index < 0 || index > transporters.size())
			return null;
		if(index == 3) {
			System.out.println(transporters.size());
		}
		return transporters.get(index)	;
	}
	
	public Transporter getTransporter(String name){
		Iterator<Transporter> it = iterator();
		while(it.hasNext()){
			Transporter t = it.next();
			if(t.getName().equals(name)){
				return t;
			}
		}
		return null;
	}
	
	public int getSize(){
		return transporters.size();
	}

	@Override
	public Iterator<Transporter> iterator() {
		return transporters.iterator();
	}

}
